package org.example;

import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        copyFile();
        copyFileByBytes();
        copyFileByCache();
        bufferWriter();
        bufferRead();
        System.out.println("Hello world!");
    }

    private static void bufferWriter() throws IOException{
        BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter("wenjian1"));
        bufferedWriter.write("今天天气不错");
        bufferedWriter.newLine();
        bufferedWriter.write("踢球！");
        bufferedWriter.newLine();
        bufferedWriter.close();
    }

    private static void bufferRead() throws IOException {
        BufferedReader bufferedReader = new BufferedReader(new FileReader("wenjian1"));
        String data;
        while((data = bufferedReader.readLine())!=null){
            System.out.println(data);
        }
    }

    private static void copyFileByCache() throws IOException{
        long start = System.currentTimeMillis();
        FileInputStream fileInputStream = new FileInputStream("lj");
        BufferedInputStream bufferedInputStream = new BufferedInputStream(fileInputStream);
        FileOutputStream fileOutputStream = new FileOutputStream("lj");
        BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream);
        int read;
        byte[] bytes = new byte[1024];

        while((read=bufferedInputStream.read(bytes))!=-1){
            bufferedOutputStream.write(bytes,0,read);
        }
        bufferedInputStream.close();
        bufferedOutputStream.close();
        fileInputStream.close();
        fileOutputStream.close();

        long end = System.currentTimeMillis();
        System.out.println("消耗时间："+(end-start));
    }

    private static void copyFileByBytes() throws IOException{
        //一次读写一个字节数组的数据
        long start = System.currentTimeMillis();
        FileInputStream fileInputStream = new FileInputStream("lj");
        FileOutputStream fileOutputStream = new FileOutputStream("lj");
        int read;
        byte[] bytes = new byte[1024];
        while((read = fileInputStream.read(bytes))!=-1){
            fileOutputStream.write(bytes,0,read);
        }
        fileInputStream.close();
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("消耗时间："+(end-start));
    }

    private static void copyFile() throws IOException {
        //一次读写一个字节数据
        long start = System.currentTimeMillis();
        FileInputStream fileInputStream = new FileInputStream("路径");
        FileOutputStream fileOutputStream = new FileOutputStream("luj");

        int read ;
        while((read = fileInputStream.read())!=-1){
            fileOutputStream.write(read);
        }
        fileInputStream.close();
        fileOutputStream.close();
        long end = System.currentTimeMillis();
        System.out.println("消耗时间为:"+(end-start));
    }

}